package cn.tede.submarine;
import javax.swing.ImageIcon;
import java.util.Random;
import java.awt.Graphics;       //
/*海洋对象 */
public abstract class SeaObject {
    public static final int LIVE = 0;//活着的
    public static final int DEAD = 1;//死了的
    protected int state = LIVE;//当前状态，默认为活着的

    protected int width;
    protected int height;
    protected int x;
    protected int y;
    protected int speed;
    //专门给侦查潜艇、水雷潜艇、鱼雷潜艇用的
    //因为三种潜艇宽高不同，所以需要传参
    public SeaObject(int width,int height){
        this.width = width;
        this.height = height;
        x = -width;
        Random rand = new Random();
        y = rand.nextInt(World.HEIGHT-height-150+1)+150;//    150 到(479窗口高-19潜艇高）之内的随机数
        speed = rand.nextInt(3)+1;//0+1到2+1的随机数
    }
    public SeaObject(int width,int height,int x,int y,int speed){
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }
    public abstract void move();
    //获取对象的图片
    public abstract ImageIcon getImage();
    //判断对象是否活着
    public boolean isLive(){
        return state == LIVE;//若当前状态为LIVE，则表示活着的，返回true，否则返回false
    }
    //判断对象是否为死的
    public boolean isDead(){
        return state == DEAD;//若当前状态为DEAD，则表示死了的，返回值为true，否则返回false
    }
    //画对象,g就是一个笔
    public void paintImage(Graphics g){
        if (isLive()){//活着才画
            //不用背，以后不用。在窗口里画的
            this.getImage().paintIcon(null,g,this.x,this.y);//重写被调看重写的
        }
    }
    //越界判断,后期要用到这个
    public boolean isOutOfBounds(){
        return x>=World.WIDTH;
    }

    //this表示当前对象，other表示另一个对象
    public boolean isHit(SeaObject other){
        //假设：this指代潜艇，other指代炸弹
            int x1 = this.x-other.width;
            int x2 = this.x+this.width;
            int y1 = this.y-other.height;
            int y2 = this.y+this.height;
            int x = other.x;
            int y = other.y;
        return x>=x1 && x<=x2
                &&
                y>=y1 && y<=y2;
    }
    public void goDead(){
        state = DEAD;
    }



}


